﻿@model Commission.Models.Product
@using MyResources = Commission.Resources.AppResource

@{
    ViewBag.Title = "Edit";
}

@section MyJavascript {
    <script type="text/javascript">
    $(document).ready(function () {
        $('#FileName').autocomplete(
        {
            source: '@Url.Action("ImageNameSearch", "Product")'
        }
        );
    })

    function showImage(fileInput) {
        var files = fileInput.files;

        for (var i = 0; i < files.length; i++) {
            var file = files[i];
            var imageType = /image.*/;
            if (!file.type.match(imageType)) {
                continue;
            }
            var imageDiv = document.getElementById("imageDiv");
            imageDiv.style.display = 'inline';

            var img = document.getElementById("thumbnail");
            img.file = file;

            $("#FileName").val(file.name);

            var reader = new FileReader();
            reader.onload = (function (aImg) {
                return function (e) {
                    aImg.src = e.target.result;
                };
            })(img);
            reader.readAsDataURL(file);
        }
    }
    </script>
}

<h2>@MyResources.LinkEdit</h2>

@using (Html.BeginForm("Edit", "Product", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>Product</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.ProductId)

        <div class="form-group">
            @Html.Label(@MyResources.LabelProductNo,
                htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ProductNo, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ProductNo, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Label(@MyResources.LabelProductName,
                htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ProductName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ProductName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Label(@MyResources.LabelImageFileName,
                htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">
                @Html.EditorFor(model => model.FileName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.FileName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Label("　",
                htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">
                <input type="File" name="imageFile" id="imageFile"
                       value="Choose File" accept="image/*" onchange="showImage(this)" />
                <br />
                <div id="imageDiv" style="display:none">
                    <img id="thumbnail" style="width:20%; margin-top:10px;" src="" alt="image" />
                    <br />
                </div>
            </div>
        </div>

        <div class="form-group">
            @Html.Label(@MyResources.LabelPrice,
                htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">
                @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Label(@MyResources.LabelUnit, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">
                @Html.DropDownList("UnitId", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.UnitId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Label(@MyResources.LabelPPPoint, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">
                @Html.EditorFor(model => model.PPPoint, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PPPoint, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Label(@MyResources.LabelNote, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">
                @Html.EditorFor(model => model.Note, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Note, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="@MyResources.LabelSave" class="btn btn-default" />
            </div>
        </div>

        @Html.HiddenFor(model => model.File)
    </div>
}

<div>
    @Html.ActionLink(@MyResources.LinkBackToList, "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
